<%@page import="com.qf.myschool.entity.Student"%>
<%@page import="java.util.List"%>
<%@page import="javax.swing.GroupLayout.Alignment"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	/*
	//第一种删除学生
	function delStu(uname1,inputObject){
		var result=confirm("确定要删除当前这个学生吗?");
		if (result) {

			var ajaxObject;
			if(window.XMLHttpRequest){//新浏览器
				ajaxObject=new XMLHttpRequest();
			}else{//Ie5,6
				ajaxObject=new ActiveXObject("Microsoft.XMLHTTP");
			}
			ajaxObject.open("get","TeacherServlet?method=delStudentByName&uname="+uname1,true );
			ajaxObject.send();
			ajaxObject.onreadystatechange=function () {
				if(ajaxObject.readyState==4&&ajaxObject.status==200){
					var result=ajaxObject.responseText;
					if(result=="1"){
						//获得表格中要删除的这一行
						var delTr=inputObject.parentNode.parentNode;
						//获得表格中要删除的这一行的父节点
						var parent=delTr.parentNode;
						parent.removeChild(delTr);
						alert("删除成功")
					}
				}
			}
			//注意:超链接和window.location传参时,只参传普通类型的值,不能直接传对象和集合.
			//window.location="提交的地址?Key1=值1&key2=值2",?是提交地址和参数的分隔符,
			//uname是key,真正传的是uname1这个变量中值
			//在servlet中无论是接收超链接参数还是window.location中参数,用request.getParameter("Key")
			//window.location="TeacherServlet?method=delStudentByName&uname="+uname1;
		}
	}*/
	//第二种删除学生
	function delStu(uname1,curr){
		var result=confirm("确定要删除当前这个学生吗?");
		if (result) {

			var ajaxObject;
			if(window.XMLHttpRequest){//新浏览器
				ajaxObject=new XMLHttpRequest();
			}else{//Ie5,6
				ajaxObject=new ActiveXObject("Microsoft.XMLHTTP");
			}
			ajaxObject.open("get","TeacherServlet?method=delStudentByName&uname="+uname1+"&currpage="+curr,true );
			ajaxObject.send();
			ajaxObject.onreadystatechange=function () {
				if(ajaxObject.readyState==4&&ajaxObject.status==200){
					var result=ajaxObject.responseText;
					document.getElementById("tb1").innerHTML=result;
					}
				}
			}
			//注意:超链接和window.location传参时,只参传普通类型的值,不能直接传对象和集合.
			//window.location="提交的地址?Key1=值1&key2=值2",?是提交地址和参数的分隔符,
			//uname是key,真正传的是uname1这个变量中值
			//在servlet中无论是接收超链接参数还是window.location中参数,用request.getParameter("Key")
			//window.location="TeacherServlet?method=delStudentByName&uname="+uname1;

	}
	//修改学生信息
	function  updateStu(uname1){
		window.location="CommonServlet?method=getStudentByName&uname="+uname1;
	}

	//ajax分页处理
	function  fenye(curr){
		//window.location="TeacherServlet?method=allStudent&currpage="+curr;
		//1.创建ajax对象
		var ajaxObject;
		if(window.XMLHttpRequest){//新版浏览器
			ajaxObject=new XMLHttpRequest();
		}else {//IE5,6
			ajaxObject=new ActiveXObject("Microsoft.XMLHTTP")
		}
		//2.创建请求
		ajaxObject.open("get","TeacherServlet?method=allStudent&currpage="+curr,true);
		//3.发送ajax请求
		ajaxObject.send();
		//4.调用回调函数，接受响应数据
		ajaxObject.onreadystatechange=function () {
			if(ajaxObject.readyState==4&&ajaxObject.status==200){
				//声明变量接受响应的嵌入数据的模板页面
				var result=ajaxObject.responseText;
				//用接收的嵌入数据的模板页面替换当前页面
				document.getElementById("tb1").innerHTML=result;

			}
		}
	}
</script>
</head>
<body>
<div id="tb1">
	<table border="1">
		<tr>
			<td>姓名</td>
			<td>性别</td>
			<td>邮箱</td>
			<td>爱好</td>
			<td>出生城市</td>
			<td>操作</td>
		</tr>
		<c:forEach var="s" items="${requestScope.stuList}">
			<tr>
					<td>${s.name}</td>
					<td>${s.sex}</td>
					<td>${s.email}</td>
					<td>${s.hobby}</td>
					<td>${s.city}</td>
					<td>
						<input type="button" value="修改" onclick="updateStu('${s.name}')"/>
						<input type="button" value="删除" onclick="delStu('${s.name}','${page1.currPage}')"/>
					</td>
			</tr>
		</c:forEach>
		<tr><td colspan="6"><a href="TeacherServlet?method=add">添加学生信息</a></td></tr>
		<tr><td colspan="6">
			<c:if test="${page1.currPage>1}">
				<input type="button" value="上一页" onclick="fenye(${page1.currPage-1})"/>
			</c:if>
			当前是第${page1.currPage}页
			<c:if test="${page1.currPage<page1.totalPage}" >
				<input type="button" value="下一页" onclick="fenye(${page1.currPage+1})"/>
			</c:if>
		</td></tr>
		<tr><td colspan="6">
			<h1>
				<c:forEach begin="1" end="${page1.totalPage }" step="1" varStatus="status">
					<a href="javaScript:void(0)" onclick="fenye(${status.index})">${status.index}</a>
				</c:forEach>
			</h1>
		</td></tr>
	</table>
</div>
</body>
</html>